﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AutoLoL.Logic.Utils;

namespace AutoLoL.Playground
{
    /// <summary>
    /// Interaction logic for GameMemoryWindow.xaml
    /// </summary>
    public partial class GameMemoryWindow : Window
    {
        public GameMemoryWindow()
        {
            InitializeComponent();
            this.DataContext = new GameMemoryViewModel();
        }
    }

    public class GameMemoryViewModel : DependencyObject
    {
        public TimeSpan GameTime
        {
            get { return (TimeSpan)GetValue(GameTimeProperty); }
            set { SetValue(GameTimeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for GameTime.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GameTimeProperty =
            DependencyProperty.Register("GameTime", typeof(TimeSpan), typeof(GameMemoryViewModel));


        public GameMemoryViewModel()
        {
            GameTime = new TimeSpan(0, 0, 0, 0);

            BackgroundWorker bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;
            bw.DoWork += delegate(object sender, DoWorkEventArgs e)
                             {
                                 MemoryManager mm = null;
                                 while (mm == null)
                                 {
                                     try
                                     {
                                         mm = new MemoryManager("League of Legends");
                                     }
                                     catch
                                     {
                                         if (Debugger.IsAttached) throw;
                                     }
                                
                                 }
                                 while (!e.Cancel)
                                 {

                                     int result = mm.ReadOffset(0x05489484, 0x08);

                                     bw.ReportProgress(0, new TimeSpan(0, 0, result));
                                     Thread.Sleep(100);
                                 }
                             };

            bw.ProgressChanged += delegate(object sender, ProgressChangedEventArgs e)
                                      {
                                          TimeSpan ts = (TimeSpan)e.UserState;
                                          GameTime = ts;
                                      };

            bw.RunWorkerAsync();

        }
    }
}
